<?
session_start();
include("../../../webkit/include/simple_db.php");

require("ajax_base.php");

$db = new SimpleDB();
$action = $_GET['action'];
$admin_agent_ids = array("kqyb01", "kqyb02", "kqyb03");

if($action == "del") {
	$db->doDelete('kqyb_callinfo', 'id='.$_GET['id']); 
	echo "ok";

} else if($action == "list") {
	$condition = "1=1";
	if(isset($_GET['telephone'])) {
		$condition .= " and client_source=".$_GET['client_source'];
	}
	if(isset($_GET['cust_name'])) {
		$condition .= " and client_name like '%".$_GET['client_name']."%'";
	}
	if( ! in_array($_SESSION['kqyb_agent_id'], $admin_agent_ids) ){
		$condition .= " and (b.agent_id='".$_SESSION['kqyb_agent_id']."' or b.agent_id='')";
	}
	$sql = "select a.id,cust_name,cust_sex,cust_tel,cust_addr,call_type, call_tel, call_result, biz_type, call_content, agent_id, call_time from kqyb_callinfo a Left Join kqyb_customer b 
	 ON a.telephone=b.cust_tel where $condition order by a.id desc"; 
	// echo $sql;
	echo to_grid_json($db, $sql, $dic_data, array("cust_name"=>"blue_color_mapper","cust_tel"=>"cust_tel_mapper"));

} else if($action == "get_customer") {
	$cust_tel = $_GET['cust_tel'];
	if($db->getOneValue("select id from kqyb_customer where cust_tel='$cust_tel'") == null) {
		echo "new customer";
	} else {
		$sql = "select * from kqyb_customer where cust_tel='$cust_tel'"; 
		$arr = $db->getOneRecordArray($sql);
		echo array_to_json($arr);
	}

} else if($action == "get_record") {
	$id = $_GET['id'];
	$sql = "select a.id,a.telephone,a.call_tel,a.call_time,a.call_type,a.call_content,a.biz_type,a.call_result, 
	b.cust_name,b.cust_tel,b.cust_sex,b.cust_desc,b.cust_addr from kqyb_callinfo a Left Join kqyb_customer b On a.telephone=b.cust_tel where a.id=$id"; 
	$arr = $db->getOneRecordArray($sql);
	echo array_to_json($arr);

} else if($action == "add") {  
	$cust_tel = $_POST['cust_tel'];
	if($db->getOneValue("select id from kqyb_customer where cust_tel='$cust_tel'") == null) {
		// 首次保存用户信息
		$db->doInsert('kqyb_customer', array("cust_name"=>$_POST['cust_name'], "cust_tel"=>$cust_tel, "cust_desc"=>$_POST['cust_desc'], "cust_sex"=>$_POST['cust_sex'], "cust_addr"=>$_POST['cust_addr'], "agent_id"=>$_POST['agent_id'], "post_time"=>date("Y-m-d H:i:s")));
	} else {
		// 更新用户信息(除电话外)
		$db->doUpdate('kqyb_customer', array("cust_name"=>$_POST['cust_name'], "cust_desc"=>$_POST['cust_desc'], "cust_sex"=>$_POST['cust_sex']), 
		"cust_tel='$cust_tel'");
	}
	$db->doInsert('kqyb_callinfo', array("call_tel"=>$_POST['call_tel'], "telephone"=>$cust_tel, "call_type"=>$_POST['call_type'], "biz_type"=>$_POST['biz_type'], "call_time"=>$_POST['call_time'], "call_result"=>$_POST['call_result'], "call_content"=>$_POST['call_content']));
	echo 'ok';

}  else if($action == "edit") { // 按呼入记录进行修改操作
	$id = $_POST['id'];
	$user_id = $db->getOneValue("select b.id from kqyb_callinfo a Left Join kqyb_customer b On a.telephone=b.cust_tel where a.id=".$id);
	// 更新用户信息(可以修改电话)
	$db->doUpdate('kqyb_customer', array("cust_name"=>$_POST['cust_name'], 'cust_tel'=>$_POST['cust_tel'], "cust_desc"=>$_POST['cust_desc'], "cust_sex"=>$_POST['cust_sex'], "cust_addr"=>$_POST['cust_addr'], "agent_id"=>$_POST['agent_id']),  "id=$user_id");
	// 更新通话信息
	$db->doUpdate('kqyb_callinfo', array("call_tel"=>$_POST['call_tel'], "telephone"=>$_POST['cust_tel'], "call_type"=>$_POST['call_type'], "biz_type"=>$_POST['biz_type'], "call_result"=>$_POST['call_result'], "call_content"=>$_POST['call_content']), "id=".$id);
	echo 'ok';

}


function cust_tel_mapper($v, $arr) {
	return "<font color=red>$v</font>&nbsp;<iframe src=\'http://222.240.139.227:9080/agent_event/callout_button.jsp?caller=".$arr['call_tel']."&called=$v&iconSize=15\' width=16 height=16 frameborder=0 marginheight=0 marginwidth=0 scrolling=no align=top></iframe>";
}


?>